package adv7;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ChatThread extends Thread {
	private Socket s;

	public ChatThread(Socket s) {
		this.s = s;
	}

	@Override
	public void run() {
		System.out.println("客户端连接了，信息是：" + s.getInetAddress() + "," + s.getPort() + "," + s.getLocalPort());
		try {
			InputStream in = s.getInputStream();
			OutputStream out = s.getOutputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			PrintWriter pw = new PrintWriter(out, true);// 第二个参数，自动刷新
			String str = null;
			while ((str = br.readLine()) != null) {
				System.out.println("client say:" + str);
				pw.println("你说的事：" + str + ",朕知道了。。。");
				// pw.flush();
				if (str.equals("exit"))
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				s.close();
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				// e.printStackTrace();
			}
		}
	}
}
